package cc.xuanhaojituan.mybatis.service;

import cc.xuanhaojituan.mybatis.util.GenericMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public abstract class GenericService<T> {
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void save(T model) {
		getMapper().insertSelective(model);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void save(List<T> models) {
		getMapper().insertList(models);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteById(Integer id) {
		getMapper().deleteByPrimaryKey(id);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteByExample(Example example) {
		getMapper().deleteByExample(example);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void update(T model) {
		getMapper().updateByPrimaryKeySelective(model);
	}

	public T findById(Integer id) {
		return getMapper().selectByPrimaryKey(id);
	}

	public List<T> findByExample(Example example) {
		return getMapper().selectByExample(example);
	}

	public List<T> findAll() {
		return getMapper().selectAll();
	}
	
	abstract GenericMapper<T> getMapper();

}
